package com.kim.androidbase.netstate;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;

public class NetState {

	private static final String TAG = "NetState";

	/**
	 * 网络已连接
	 */
	public static final int NETSTATE_CONNECTED = 0;
	/**
	 * 网络未连接
	 */
	public static final int NETSTATE_DISCONNECTED = 1;

	/**
	 * WiFi
	 */
	public static final int NETTYPE_WIFI = 0;
	/**
	 * 非WiFi
	 */
	public static final int NETTYPE_NOT_WIFI = 1;

	/**
	 * 未知
	 */
	public static final int NETTYPE_UNKONW = -1;

	/**
	 * 网络连接状态
	 */
	private static int netState;

	/**
	 * 网络类型
	 */
	private static int netType;

	/**
	 * 信号强度
	 */
	private static int signalstrength;

	private static ConnectivityManager connectivityManager;

	private static TelephonyManager telephonyManager;

	private static class MyPhoneStateListener extends PhoneStateListener {

		@Override
		public void onSignalStrengthChanged(int asu) {
			super.onSignalStrengthChanged(asu);
			Log.d(TAG, "onSignalStrengthChanged::asu::" + asu);
		}

		@Override
		public void onSignalStrengthsChanged(SignalStrength signalStrength) {
			super.onSignalStrengthsChanged(signalStrength);
			Log.d(TAG, "onSignalStrengthsChanged::signalStrength::"
					+ signalStrength.getGsmSignalStrength());
			signalstrength = signalStrength.getGsmSignalStrength();
		}

	}

	public static void refreshNetState(Context context) {

		if (connectivityManager == null) {
			connectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
		}

		if (telephonyManager == null) {
			telephonyManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			telephonyManager.listen(new MyPhoneStateListener(),
					PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
		}

		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null || !networkInfo.isAvailable()) {
			netState = NETSTATE_DISCONNECTED;
			netType = NETTYPE_UNKONW;
			return;
		}

		netState = NETSTATE_CONNECTED;
		if (ConnectivityManager.TYPE_MOBILE == networkInfo.getType()) {
			netType = NETTYPE_NOT_WIFI;
		} else if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
			netType = NETTYPE_WIFI;
		} else {
			netType = NETTYPE_UNKONW;
		}
	}

	public static int getNetState() {
		return netState;
	}

	protected static void setNetState(int netState) {
		NetState.netState = netState;
	}

	public static int getNetType() {
		return netType;
	}

	protected static void setNetType(int netType) {
		NetState.netType = netType;
	}

	public static int getSignalStrength() {
		return signalstrength;
	}

	protected static void setSignalStrength(int signalStrength) {
		NetState.signalstrength = signalStrength;
	}

}
